自定义View预览

自定义 View 预览

isInEditMode

RecyclerView:

private void createLayoutManager(Context context, String className, AttributeSet attrs,  
        int defStyleAttr, int defStyleRes) {  
    if (className != null) {  
        className = className.trim();  
        if (!className.isEmpty()) {  
            className = getFullClassName(context, className);  
            try {  
                ClassLoader classLoader;  
                if (isInEditMode()) {  
                    // Stupid layoutlib cannot handle simple class loaders.  
                    classLoader = this.getClass().getClassLoader();  
                } else {  
                    classLoader = context.getClassLoader();  
                }  
                Class<? extends LayoutManager> layoutManagerClass =  
                        Class.forName(className, false, classLoader)  
                                .asSubclass(LayoutManager.class);  
                Constructor<? extends LayoutManager> constructor;  
                Object[] constructorArgs = null;  
                try {  
                    constructor = layoutManagerClass  
                            .getConstructor(LAYOUT_MANAGER_CONSTRUCTOR_SIGNATURE);  
                    constructorArgs = new Object[]{context, attrs, defStyleAttr, defStyleRes};  
                } catch (NoSuchMethodException e) {  
                    try {  
                        constructor = layoutManagerClass.getConstructor();  
                    } catch (NoSuchMethodException e1) {  
                        e1.initCause(e);  
                        throw new IllegalStateException(attrs.getPositionDescription()  
                                + ": Error creating LayoutManager " + className, e1);  
                    }  
                }  
                constructor.setAccessible(true);  
                setLayoutManager(constructor.newInstance(constructorArgs));  
            } catch (ClassNotFoundException e) {  
            // ... 
        }  
    }  
}